//
//  TZYMineColletionViewController.m
//  Moment
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 片刻. All rights reserved.
//

#import "TZYMineColletionViewController.h"
#import "TZYMineColletionCell.h"


@interface TZYMineColletionViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic,assign)CGFloat cellHeight;
@property (nonatomic, retain)MBProgressHUD *hud;
@property (nonatomic, strong) UITableView *myTableView;

@end

@implementation TZYMineColletionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    self.navigationItem.title = @"收藏";
    self.array = [NSMutableArray array];
     self.array = [[CollectViewController collectDataBase]selectCollectList];
   
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:self.myView.bounds];
    myImageView.image = [UIImage imageNamed:@"1.jpg"];
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
    blurView.frame = myImageView.bounds;
    blurView.alpha = 0.6;
    [myImageView addSubview:blurView];
    [self.myView addSubview:myImageView];
    [self.view addSubview:self.myView];
    
    
    
    [self createTableView];
    
    
    UIImage *image = [UIImage imageNamed:@"back.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
}


- (void)leftButtonAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)createTableView
{
    self.myTableView = [[UITableView alloc] init];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.showsVerticalScrollIndicator = NO;
    self.myTableView.backgroundColor = [UIColor whiteColor];
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    //self.myTableView.rowHeight = GoodsCellHeight;
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[TZYMineColletionCell class] forCellReuseIdentifier:@"TZYMineColletionCell"];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return MineColletionCellHeight;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TZYMineColletionCell";
    TZYMineColletionCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    

    TZYHomeData *homeM = [self.array objectAtIndex:indexPath.row];
    cell.myModel = homeM;

    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}



#pragma mark -- 每行的编辑样式(删除)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
}


#pragma mark - 删除编辑逻辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //如果是进行删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //先删除表中的数据 后删除cell
        TZYHomeData *model = [self.array objectAtIndex:indexPath.row];
        //删除表中的数据
        [[CollectViewController collectDataBase]deleteMyCollectData:model.title];
        
        
        [self.array removeObjectAtIndex:indexPath.row];
        
        if (self.array.count == 0) {
            [self.view addSubview:self.myView];
            
            self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.hud.labelText = @"什么都没有收藏呢";
            self.hud.mode = MBProgressHUDModeText;
            [self.hud hide:YES afterDelay:1];
        }
        
        
        //删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    }
    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
